﻿using System;
using System.Collections.Generic;
using System.Linq;
using Hxsoft.AppV1;

namespace Hxsoft.AppV1.Module
{
	/// <summary>
	/// InfoResourceCopies 类。
	/// </summary>
	public static partial class InfoResourceCopies
	{
		#region GetItem
		/// <summary>
		/// 根据标识获取指定的信息资源副本。
		/// </summary>
		/// <param name="o">信息资源副本集合。</param>
		/// <param name="resourceId">获取的信息资源副本标识 ResourceId。</param>
		/// <param name="copyName">获取的信息资源副本标识 CopyName。</param>
		/// <returns>获取的信息项。</returns>
		public static InfoResourceCopy GetItem(this System.Data.Linq.Table<InfoResourceCopy> o, int resourceId, string copyName)
		{
			return o.SingleOrDefault(p => p.ResourceId == resourceId && p.CopyName == copyName);
		}
		#endregion



		#region Exists
		/// <summary>
		/// 验证指定的信息资源副本是否已经存在。
		/// </summary>
		/// <param name="o">信息资源副本集合。</param>
		/// <param name="resourceId">验证的信息资源副本标识 ResourceId。</param>
		/// <param name="copyName">验证的信息资源副本标识 CopyName。</param>
		/// <returns>布尔值表示指定的信息资源副本是否已经存在。</returns>
		public static bool Exists(this System.Data.Linq.Table<InfoResourceCopy> o, int resourceId, string copyName)
		{
			return o.Any(p => p.ResourceId == resourceId && p.CopyName == copyName);
		}
		#endregion
	}



	/// <summary>
	/// InfoResourceCopies 类操作部分。
	/// </summary>
	public static partial class InfoResourceCopies
	{
		#region GenerateCopy
		public static InfoResourceCopy GenerateCopy(int resourceId, string copyName, IResourceCopyGenerator generator)
		{
			GenericModuleDataContext dc2 = new GenericModuleDataContext();
			dc2.ObjectTrackingEnabled = false;

			InfoResourceCopy copy = dc2.InfoResourceCopies.GetItem(resourceId, copyName);
			if (copy != null) return copy;

			return Insert(dc2.InfoResources.GetItem(resourceId), copyName, generator);
		}

		public static InfoResourceCopy GenerateCopy(InfoResource resource, string copyName, IResourceCopyGenerator generator)
		{
			GenericModuleDataContext dc2 = new GenericModuleDataContext();
			dc2.ObjectTrackingEnabled = false;

			InfoResourceCopy copy = dc2.InfoResourceCopies.GetItem(resource.ResourceId, copyName);
			if (copy != null) return copy;

			return Insert(resource, copyName, generator);
		}
		#endregion



		#region Insert
		internal static InfoResourceCopy Insert(InfoResource resource, string copyName, IResourceCopyGenerator generator)
		{
			// 生成副本
			GenericModuleDataContext dc = new GenericModuleDataContext();
			InfoResourceCopy copy = new InfoResourceCopy();
			copy.ResourceId = resource.ResourceId;
			copy.CopyName = copyName;
			string copyType = resource.ResourceType;

			if (resource.Content != null)
			{
				copy.CopyContent = generator.Generate(resource.Content.ToArray(), ref copyType);
				copy.CopyType = copyType;
				copy.CopySize = copy.CopyContent.Length;
			}
			copy.CreationDate = DateTime.Now;
			dc.InfoResourceCopies.InsertOnSubmit(copy);
			dc.SubmitChanges();

			// 返回只读数据。
			return copy.ToNewEntity<InfoResourceCopy>();
		}
		#endregion



		#region Delete
		#region internal Delete
		internal static void Delete(GenericModuleDataContext dc, InfoResourceCopy copy)
		{
			dc.InfoResourceCopies.DeleteOnSubmit(copy);
		}

		internal static void DeleteByParent(GenericModuleDataContext dc, InfoResource parent)
		{
			IEnumerable<InfoResourceCopy> e = parent.InfoResourceCopies.AsEnumerable();
			foreach (InfoResourceCopy i in e)
				Delete(dc, i);
		}
		#endregion
		#endregion
	}
}